const Generator = require('yeoman-generator')

module.exports = class extends Generator {
  // 以交互的方式询问用户一些问题
  prompting() {
    return this.prompt([
      {
        type: 'input',
        name: 'name',
        message: 'Your project name?',
        default: this.appname,
      },
    ]).then((answers) => {
      this.answers = answers
    })
  }

  // 用 prompting 过程中用户的回答替换掉模版中对应的变量，并将模版文件写入目标工程目录
  writing() {
    const templates = [
      '.browserslistrc',
      '.editorconfig',
      '.env.development',
      '.env.production',
      '.eslintrc.js',
      '.gitignore',
      'babel.config.js',
      'package.json',
      'postcss.config.js',
      'README.md',
      'public/favicon.ico',
      'public/index.html',
      'src/App.vue',
      'src/main.js',
      'src/router.js',
      'src/assets/logo.png',
      'src/components/HelloWorld.vue',
      'src/store/actions.js',
      'src/store/getters.js',
      'src/store/index.js',
      'src/store/mutations.js',
      'src/store/state.js',
      'src/utils/request.js',
      'src/views/About.vue',
      'src/views/Home.vue',
    ]

    templates.forEach((element) => {
      this.fs.copyTpl(
        this.templatePath(element),
        this.destinationPath(element),
        this.answers
      )
    })
  }
}
